home *** CD-ROM | disk | FTP | other *** search
/ El Mac 9 / El Mac 9.iso / Shareware / Demos / Igor Demo Pro / 1 PutContentsIn Igor Pro Folder / WaveMetrics Procedures / Utilities / String Utilities / Keyword-Value next >
Encoding:
Text File  |  1994-02-18  |  2.1 KB  |  93 lines  |  [TEXT/IGR0]

  1. | <Keyword-Value>
  2. |
  3. | parses "key:value;key2:value2;" list
  4. | Returns the string value string given the corresponding key
  5. |
  6. Function/S StrByKey(key,str)
  7.     String key,str
  8.     
  9.     key += ":"
  10.     Variable pos= strsearch(str, key, 0)
  11.     if( pos < 0 )
  12.         return ""
  13.     endif
  14.     pos += strlen(key)
  15.     Variable pos2= strsearch(str,";",pos)
  16.     if (pos2 == -1)                | this is the last value in list ?
  17.         pos2 = strlen(str)
  18.     endif
  19.     return str[pos,pos2-1]
  20. End
  21.  
  22. | parses "key:value;key2:value2;" list
  23. | Returns the numeric value of the string given the corresponding key, or NaN
  24. |
  25. Function/D NumByKey(key,str)
  26.     String key,str
  27.     
  28.     String s= StrByKey(key,str)
  29.     if( strlen(s) == 0 )
  30.         return NaN
  31.     endif
  32.     return str2num(s)
  33. End
  34.  
  35. | Replaces "<key>:<str>;" in the list, or adds it to the start of the list
  36. | list is usually a global string containing lots of settings,
  37. | each setting with a unique key.
  38. | Note: we ASSUME that key and str do not contain either the ':' or ';' character
  39. | (if it does, the list is damaged). Any other character, however, is okay.
  40. | Returns the new list.
  41. |
  42. | Usage: listStr= ReplaceStrByKey(listStr,"DayOfWeek","Monday")
  43. |
  44. Function/S ReplaceStrByKey(list,key,str)    
  45.     String list,key,str
  46.  
  47.     key += ":"
  48.     Variable pos2=0,pos= strsearch(list, key, 0)
  49.     if( pos >= 0 )
  50.         pos += strlen(key)
  51.         pos2= strsearch(list,";",pos)
  52.         list[pos,pos2-1]=str
  53.     else
  54.         list[-1]=key+str+";"    | Last-In-First-Out
  55.     endif
  56.     return list
  57. End
  58.  
  59. | Replaces "<key>:<value>;" in the list, or adds it to the start of the list
  60. | See ReplaceStrByKey
  61. | Returns the new list.
  62. |
  63. | Usage: listStr= ReplaceNumByKey(listStr,"angle of attack",3.14159/16)
  64. |
  65. Function/S ReplaceNumByKey(list,key,num)
  66.     String list,key
  67.     Variable/D num
  68.     
  69.     String valueAsString
  70.     sprintf valueAsString,"%.15g",num
  71.     return ReplaceStrByKey(list,key,valueAsString)
  72. End
  73.  
  74. | Removes "<key>:<str>;" from the list, if it exists.
  75. | See ReplaceStrByKey
  76. | Returns the new list.
  77. |
  78. | Usage: listStr= DeleteByKey(listStr,"key to delete")
  79. |
  80. Function/S DeleteByKey(list,key)    
  81.     String list,key
  82.  
  83.     key += ":"
  84.     Variable pos2,pos= strsearch(list, key, 0)
  85.     if( pos >= 0 )
  86.         pos2= strsearch(list,";",pos)
  87.         list[pos,pos2]=""
  88.     endif
  89.     return list
  90. End
  91.  
  92.  
  93.